/**
 * User 业务逻辑实现类
 * @author yanwl
 * @date 2015-03-20
 */
package com.maven.demo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maven.demo.core.common.DaoHelper;
import com.maven.demo.model.pojo.User;
import com.maven.demo.service.UserService;
import com.maven.demo.service.listener.UserListenerRegister;


@Service("userService")
@Transactional
public class UserServiceImpl  implements UserService {
	//声明工作空间
	private final static String nameSpace = "com.maven.demo.mapper.UserMapper";
	
	@Autowired
	private UserListenerRegister userListenerRegister;
		
	@Override
	public boolean insertUser(User user) {
		return DaoHelper.insert(nameSpace, "insertUser", user);
	}

	@Override
	public boolean updateUser(User user) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("field", "user_id");
		map.put("value", user.getUserId());
		User oldUser = selectUser(map);
		boolean flag = DaoHelper.update(nameSpace, "updateUser", user);
		userListenerRegister.adviceUpdate(oldUser, user);
		return flag;
	}

	@Override
	public User selectUser(Map<String, Object> map) {
		return (User)DaoHelper.query(nameSpace, "selectUser", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> selectAllUsers() {
		return (List<User>)DaoHelper.list(nameSpace, "selectAllUsers", "");
	}

	@Override
	public boolean deleteUser(Integer userId) {
		return DaoHelper.delete(nameSpace, "deleteUser", userId);
	}
}
